<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<div class="parameter-table flex h-full gap-x-3">
    <div class="flex basis-2/3 flex-col gap-y-3">
        @if (canAddParameters && !isDisabled) {
            <div class="flex justify-end items-center">
                <button mat-icon-button class="primary-icon-button" type="button" (click)="newParameterClicked()">
                    <i class="fa fa-plus"></i>
                </button>
            </div>
        }
        <div class="listing-table flex-1 relative">
            <div class="absolute inset-0 overflow-y-auto overflow-x-hidden">
                <table
                    mat-table
                    [dataSource]="dataSource"
                    matSort
                    matSortDisableClear
                    (matSortChange)="sortData($event)"
                    [matSortActive]="initialSortColumn"
                    [matSortDirection]="initialSortDirection">
                    <!-- Name Column -->
                    <ng-container matColumnDef="name">
                        <th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
                        <td mat-cell *matCellDef="let item">
                            <div class="flex justify-between items-center">
                                <div
                                    class="whitespace-nowrap overflow-hidden text-ellipsis leading-normal"
                                    [title]="item.originalEntity.parameter.name">
                                    {{ item.originalEntity.parameter.name }}
                                </div>
                                <div class="flex gap-x-2 justify-end">
                                    @if (hasDescription(item)) {
                                        <i
                                            class="fa fa-info-circle primary-color"
                                            nifiTooltip
                                            [tooltipComponentType]="TextTip"
                                            [tooltipInputData]="getDescription(item)"
                                            [delayClose]="false"></i>
                                    }
                                    @if (canOverride(item)) {
                                        <i
                                            class="fa fa-level-down primary-color"
                                            nifiTooltip
                                            [tooltipComponentType]="TextTip"
                                            [tooltipInputData]="getInheritedParameterMessage(item)"
                                            [delayClose]="false"></i>
                                    }
                                </div>
                            </div>
                        </td>
                    </ng-container>

                    <!-- Value Column -->
                    <ng-container matColumnDef="value">
                        <th mat-header-cell *matHeaderCellDef>Value</th>
                        <td mat-cell *matCellDef="let item">
                            <ng-container
                                *ngTemplateOutlet="renderValue; context: { $implicit: getValue(item) }"></ng-container>
                            <ng-template #renderValue let-parameterValue>
                                @if (isNull(parameterValue)) {
                                    <div class="unset neutral-color">No value set</div>
                                } @else {
                                    <ng-container
                                        *ngTemplateOutlet="
                                            isSensitiveParameter(item) ? sensitive : nonSensitive;
                                            context: { $implicit: parameterValue }
                                        "></ng-container>
                                    <ng-template #sensitive>
                                        <div class="sensitive neutral-color">Sensitive value set</div>
                                    </ng-template>
                                    <ng-template #nonSensitive let-value>
                                        <ng-container
                                            *ngTemplateOutlet="
                                                isEmptyString(value) ? blank : nonBlank;
                                                context: { $implicit: value }
                                            "></ng-container>
                                    </ng-template>
                                    <ng-template #blank>
                                        <div class="empty neutral-color">Empty string set</div>
                                    </ng-template>
                                    <ng-template #nonBlank let-value>
                                        <div class="flex justify-between items-center">
                                            <div
                                                class="whitespace-nowrap overflow-hidden text-ellipsis"
                                                nifiTooltip
                                                [tooltipComponentType]="TextTip"
                                                [tooltipInputData]="value">
                                                {{ value }}
                                            </div>
                                            @if (hasExtraWhitespace(value)) {
                                                <div
                                                    class="fa fa-info-circle primary-color"
                                                    nifiTooltip
                                                    [tooltipComponentType]="TextTip"
                                                    tooltipInputData="The specified value contains leading and/or trailing whitespace character(s). This could produce unexpected results if it was not intentional."></div>
                                            }
                                        </div>
                                    </ng-template>
                                }
                            </ng-template>
                        </td>
                    </ng-container>

                    <!-- Actions Column -->
                    <ng-container matColumnDef="actions">
                        <th mat-header-cell *matHeaderCellDef></th>
                        <td mat-cell *matCellDef="let item">
                            <div class="flex items-center justify-end gap-x-2">
                                @if (
                                    canGoToParameter(item) ||
                                    (canEdit(item) && !isDisabled) ||
                                    (canDelete(item) && !isDisabled)
                                ) {
                                    <button
                                        mat-icon-button
                                        type="button"
                                        [matMenuTriggerFor]="actionMenu"
                                        class="h-16 w-16 flex items-center justify-center icon global-menu">
                                        <i class="fa fa-ellipsis-v"></i>
                                    </button>
                                }
                                <mat-menu #actionMenu="matMenu" xPosition="before">
                                    @if (canGoToParameter(item)) {
                                        <button mat-menu-item [routerLink]="getParameterLink(item)">
                                            <i class="fa fa-long-arrow-right primary-color mr-2"></i>
                                            Go to
                                        </button>
                                    }
                                    @if (canOverride(item)) {
                                        <button mat-menu-item (click)="overrideParameter(item)">
                                            <i class="fa mr-2"></i>
                                            Override
                                        </button>
                                    }
                                    @if (canEdit(item) && !isDisabled) {
                                        <button mat-menu-item (click)="editClicked(item)">
                                            <i class="fa fa-cog primary-color mr-2"></i>
                                            Edit
                                        </button>
                                    }
                                    @if (canDelete(item) && !isDisabled) {
                                        <button mat-menu-item (click)="deleteClicked(item)">
                                            <i class="fa fa-trash primary-color mr-2"></i>
                                            Delete
                                        </button>
                                    }
                                </mat-menu>
                            </div>
                        </td>
                    </ng-container>

                    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                    <tr
                        mat-row
                        *matRowDef="let row; let even = even; columns: displayedColumns"
                        (click)="selectParameter(row)"
                        (dblclick)="doubleClicked(row)"
                        [class.selected]="isSelected(row)"
                        [class.even]="even"></tr>
                </table>
            </div>
        </div>
        @if (inheritsParameters) {
            <div class="flex justify-between items-center">
                <mat-checkbox [(ngModel)]="showInheritedParameters" (click)="initFilter()">
                    <mat-label class="checkbox-label">Show inherited parameters</mat-label>
                </mat-checkbox>
            </div>
        }
    </div>
    <div class="flex basis-1/3 flex-col justify-between gap-y-3 w-full">
        <div class="flex flex-col">
            <div>Parameter</div>
            <div class="tertiary-color font-medium">
                @if (selectedItem) {
                    <span>{{ selectedItem.originalEntity.parameter.name }}</span>
                } @else {
                    <span class="unset neutral-color">None</span>
                }
            </div>
        </div>
        <div class="flex-1 flex flex-col">
            <div>
                Referencing Components
                <i
                    class="fa fa-info-circle primary-color"
                    nifiTooltip
                    [tooltipComponentType]="TextTip"
                    tooltipInputData="Components referencing this parameter grouped by process group."></i>
            </div>
            <div class="relative h-full border">
                <div class="absolute inset-0 overflow-y-auto p-1">
                    @if (selectedItem) {
                        <parameter-references
                            [parameterReferences]="
                                selectedItem.originalEntity.parameter.referencingComponents
                            "></parameter-references>
                    }
                </div>
            </div>
        </div>
    </div>
</div>
